package com.example.oracle.controller;

import com.example.oracle.pojo.Info;
import com.example.oracle.pojo.User;
import com.example.oracle.pojo.system.ResponseMap;
import com.example.oracle.pojo.system.WebCode;
import com.example.oracle.service.InfoService;
import com.example.oracle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Size
 * @date 2021/12/5
 */
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    InfoService infoService;
    @Autowired
    UserService userService;

    /**
     * 用户修改信息
     * @param info 用户信息
     * @return 操作是否成功
     */
    @PostMapping("/userProfile")
    ResponseMap userProfile(@RequestBody Info info){
        return infoService.insertInfo(info) == 0 ? ResponseMap.create(WebCode.REQUEST_FAIL) : ResponseMap.create(WebCode.REQUEST_SUCCESS);
    }

    /**
     * 查询用户信息
     * @return 姓名、电话号码
     */
    @PostMapping("/getUserName")
    ResponseMap getUserName(@RequestBody User user){
        List<User> users = userService.getUsers(user);
        if (users.size() == 0 || user.getId() == 0){
            return new ResponseMap(WebCode.REQUEST_FAIL);
        }
        ResponseMap map = ResponseMap.create(WebCode.REQUEST_SUCCESS);
        map.putValue("name", users.get(0).getName());
        map.putValue("phone",users.get(0).getPhone());
        return map;
    }

    /**
     * 查询用户信息
     * @return 用户的个人信息
     */
    @GetMapping("/userProfile")
    ResponseMap userProfile(){
        Info info = new Info();
        info.setId(Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName()));
        List<Info> selects = infoService.selects(info);
        ResponseMap responseMap = new ResponseMap(WebCode.REQUEST_SUCCESS);
        if (selects.size() == 0){
            responseMap.setMsg("数据为空");
        }else {
            responseMap.putValue("info", selects.get(0));
        }
        return responseMap;
    }

    /**
     * 获取所有用户
     * @return 用户列表
     */
    @GetMapping("/allUsers")
    ResponseMap getUsers(){
        List<User> users = userService.getUsers(null);
        users.forEach((a)->{
            a.setPwd("******");
        });
        ResponseMap responseMap = ResponseMap.create(WebCode.REQUEST_SUCCESS);
        responseMap.putValue("list", users);
        return responseMap;
    }

    /**
     * 用户账号状态管理
     * @param map 管理的账号id和账号的状态
     */
    @PostMapping("/disableUser")
    ResponseMap disableUser(@RequestBody Map<String,String> map){
        Long userId = Long.parseLong(map.get("id"));
        int used = Integer.parseInt(map.get("used"));
        System.out.println(userId+""+used);
//        long l = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName());
//        if (l != 1){
//            return ResponseMap.create(WebCode.REQUEST_FAIL);
//        }
        userService.disableUser(userId, used);
        return ResponseMap.create(WebCode.REQUEST_SUCCESS);
    }
}
